<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/27 17:45
 */

namespace app\admin\model;
use think\cache\driver\Redis;

/**
 * 宝箱奖品
 * Class GoldEggPrize
 * @package app\admin\model
 */
class GoldRoomEggPrize extends DataBase
{
    protected $name = 'gold_rooms_egg_prize';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'add_time';
    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * @param $page_config
     * @param $where
     * @param $order
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        $where = array();//组装搜索语句
        $data = $this
            ->view('fw_gold_rooms_egg_prize', 'id,status,default_stock,gift_id,gift_type,gift_notice,gold_egg_id,updated_at')
           // ->view('fw_gift_url', 'gift_name,diamonds', 'fw_gift_url.id = fw_gold_rooms_egg_prize.gift_id')
            ->view('fw_gold_egg', 'egg_name', 'fw_gold_egg.id = fw_gold_rooms_egg_prize.gold_egg_id')
            ->where($where)
            ->order(['fw_gold_rooms_egg_prize.status'=>'DESC','fw_gold_rooms_egg_prize.updated_at'=>'DESC'])
            ->paginate($page_config);
        foreach($data as $key => $value){
            if($value['gift_type'] == 1){
                 $giftInfo = Gift::where('id',$value['gift_id'])->field('gift_name')->find();
                $data[$key]['gift_name'] = $giftInfo['gift_name'];
//                $data[$key]['diamonds'] = $giftInfo['diamonds'];
            }
            if($value['gift_type'] == 3){
                $data[$key]['gift_name'] = Prop::where('id',$value['gift_id'])->value('prop_name');
                $data[$key]['diamonds'] = '道具';
            }

        }
        return $data;

    }

    /**
     * @user 帅帅吴
     * @date 2018/12/22
     * @time 22:12
     * @title 房间礼物添加
     */
    public function prizeSave()
    {
        $newData = input('post.');
        $res = $this->save($newData);
        return $res;
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/23
     * @time 22:16
     * @title 奖品库存的修改
     */
    public function prizeEdit()
    {
        $redis = new Redis();
        $newData = input('get.');
        $res = $this->save($newData,['id'=>$newData['id']]);

        if(!$res)  return ['code'=>-100,'msg'=>'修改失败'];

//        $prize_stock = $this->where('id','=',$newData['id'])->value('prize_stock'); //获取剩余库存
//
//        if($prize_stock != $newData['prize_stock']){   //若修改剩余库存则修改队列

            $redis->rm('gold_room_store:gold_egg_id:'.$newData['gold_egg_id']);//清除该宝箱队列
            $totalStore = $this
                ->where('gold_egg_id','=',$newData['gold_egg_id'])
                ->where('status','=', 1)
                ->sum('prize_stock');

            for ($i = 0; $i < $totalStore; $i++) {
                $len = $redis->lPush('gold_room_store:gold_egg_id:'.$newData['gold_egg_id'], 1);  //重新队列
            }

            if($len != $totalStore) return ['code'=>-100,'msg'=>'入库成功，但队列未能与库存保持一致，可能处在抽奖高峰期'];
        return ['code'=>100,'msg'=>'修改成功'];
    }

    public function changePrizeStatus()
    {
        $redis = new Redis();
        $id = input('get.id');
        $detail = $this->field('status,gold_egg_id')->where('id','=',$id)->find();

        if(!$detail) return ['code'=>-100,'msg'=>'没找到该礼物'];

        $detail['status'] = $detail['status'] == 0? 1 : 0;
        $res = $this->save(['status'=>$detail['status']],['id'=>$id]);

        if(!$res) return ['code'=>-100,'msg'=>'修改失败'];

        $redis->rm('gold_room_store:gold_egg_id:'.$detail['gold_egg_id']);//清除该宝箱队列
        $totalStore = $this
            ->where('gold_egg_id','=',$detail['gold_egg_id'])
            ->where('status','=', 1)
            ->sum('prize_stock');

        for ($i = 0; $i < $totalStore; $i++) {
            $len = $redis->lPush('gold_room_store:gold_egg_id:'.$detail['gold_egg_id'], 1);  //重新队列
        }

        if($len != $totalStore) return ['code'=>-100,'msg'=>'入库成功，但队列未能与库存保持一致，可能处在抽奖高峰期'];

        return ['code'=>100,'msg'=>'修改成功'];

    }

    //宝箱的礼物信息
    public function getGiftData($gift_type)
    {
        if($gift_type==1){
            $data = $this->field('id,gift_id')->select()->toArray();
        }else{
            $data = $this->field('id,gift_id')->where(['gift_type'=>$gift_type,'default_stock'=>0])->select()->toArray();
        }
        foreach($data as $k=>$v){
            if($gift_type == 3){
                $giftInfo = Prop::where('id','=',$v['gift_id'])->field('prop_name as gift_name')->find();
            }else{
                $giftInfo =  GiftUrl::where('id','=',$v['gift_id'])->field('gift_name,diamonds')->find();
            }
            $data[$k]['gift_name'] = $giftInfo['gift_name'];
        }
        return $data;
    }

    //宝箱礼物详情
    public function getGoldGift($gold_id){
        $map[] = ['default_stock','=',0];
        $map[] = ['status','=',1];
        $map[] = ['gold_egg_id','=',$gold_id];
        $data = $this->field('id,gift_id')->where($map)->select()->toArray();

        foreach($data as $k=>$v){

            $giftInfo =  GiftUrl::where('id','=',$v['gift_id'])->field('gift_name,diamonds')->find();
            $data[$k]['gift_name'] = $giftInfo['gift_name'];
            $data[$k]['diamonds'] = $giftInfo['diamonds'];
        }
        return $data;
    }

    //动态获取礼物或道具
    public function boxGift()
    {


        $gift_type = input('gift_type');
        $data = '';

        if($gift_type == 1){
            $giftModel = new Gift();
            $data = Gift::where('status',1)->field('id,gift_name')->order('diamonds DESC')->select();
        }
        if($gift_type == 3){
            $data = Prop::where('status',1)->field('id,prop_name as gift_name')->select();
        }


        return ['code'=>100,'data'=>$data];
    }

}
